/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.prod.mps.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.redragon.framework.hibernate.data.SnowFlake;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.springmvc.ControllerSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.erp.dataset.service.DatasetCommonService;
import com.erp.masterdata.common.service.MasterDataCommonService;
import com.erp.masterdata.material.dao.data.DataBox;
import com.erp.masterdata.material.dao.model.MdMaterial;
import com.erp.prod.mps.dao.model.ProdMpsLine;
import com.erp.prod.mps.dao.model.ProdMpsLineCO;
import com.erp.prod.mps.service.ProdMpsLineService;

@Controller
@RequestMapping("/web/prodMpsLine")
public class ProdMpsLineWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(ProdMpsLineWebController.class);
    
    //服务层注入
    @Autowired
    private ProdMpsLineService prodMpsLineService;
    @Autowired
    private MasterDataCommonService masterDataCommonService;
    @Autowired
    private DatasetCommonService datasetCommonService;
    
    @Override
    public String getExceptionRedirectUrl() {
        return this.getFormatRedirectUrl("getProdMpsLineList");
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2020-11-17 14:45:25
     * @author 
     * @param pages
     * @param prodMpsLineCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getProdMpsLineList")
    public String getProdMpsLineList(Pages pages, ProdMpsLineCO prodMpsLineCO, Model model) {
        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        pages.setMax(100);
        
        //物料
        Map materialMap = this.masterDataCommonService.getMaterialMap();
        //物料单位
        Map materialUnitMap = this.datasetCommonService.getMaterialUnit();
        //物料属性
        Map bomPropertyMap = DataBox.getBomPropertyMap();
        
        //分页查询数据
        List<ProdMpsLine> prodMpsLineList = this.prodMpsLineService.getProdMpsLineListByMpsHeadCode(pages, prodMpsLineCO);
        
        for(ProdMpsLine prodMpsLine: prodMpsLineList) {
            prodMpsLine.setMaterialName(materialMap.get(prodMpsLine.getMaterialCode()).toString());
            //获取物料信息
            MdMaterial mdMaterial = this.masterDataCommonService.getMdMaterialInfoCache(prodMpsLine.getMaterialCode());
            prodMpsLine.setMaterialUnit(materialUnitMap.get(mdMaterial.getMaterialUnit()).toString());
            prodMpsLine.setMaterialStandard(mdMaterial.getStandard());
        }
        
        //页面属性设置
        model.addAttribute("prodMpsLineList", prodMpsLineList);
        model.addAttribute("pages", pages);
        model.addAttribute("bomPropertyMap", bomPropertyMap);
        
        return "prodMps/tab/prodMpsLineTab";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2020-11-17 14:45:25
     * @author 
     * @param prodMpsLine
     * @param model
     * @return String
     *
     */
    @RequestMapping("getProdMpsLine")
    public String getProdMpsLine(ProdMpsLine prodMpsLine, Model model) {
        //物料
        Map materialMap = this.masterDataCommonService.getMaterialMap();
        //物料单位
        Map materialUnitMap = this.datasetCommonService.getMaterialUnit();
        //物料属性
        Map bomPropertyMap = DataBox.getBomPropertyMap();
        
        //查询要编辑的数据
        if(prodMpsLine!=null&&prodMpsLine.getMpsLineId()!=null) {
            prodMpsLine = this.prodMpsLineService.getDataObject(prodMpsLine.getMpsLineId());
            
            //获取物料信息
            MdMaterial mdMaterial = this.masterDataCommonService.getMdMaterialInfoCache(prodMpsLine.getMaterialCode());
            prodMpsLine.setMaterialName(materialMap.get(prodMpsLine.getMaterialCode()).toString());
            prodMpsLine.setMaterialUnit(materialUnitMap.get(mdMaterial.getMaterialUnit()).toString());
            prodMpsLine.setMaterialStandard(mdMaterial.getStandard());
        }
        
        //页面属性设置
        model.addAttribute("prodMpsLine", prodMpsLine);
        model.addAttribute("materialMap", materialMap);
        model.addAttribute("materialUnitMap", materialUnitMap);
        model.addAttribute("bomPropertyMap", bomPropertyMap);
        
        return "prodMps/pop/addLineModal";
    }
    
    
    
    /**
     * 
     * @description 获取bom物料信息
     * @date 2020年11月15日
     * @author dongbin
     * @param materialCode
     * @return
     * @return String
     *
     */
    @RequestMapping("getBomMaterialInfoAjax")
    @ResponseBody
    public String getBomMaterialInfoAjax(String materialCode) {
        String materialUnit = "";
        String materialStandard = "";
        String bomProperty = "";
        String producePreDays = "";
        
        try {
            //获取物料信息
            MdMaterial mdMaterial = this.masterDataCommonService.getMdMaterialInfoCache(materialCode);
            
            materialUnit = this.datasetCommonService.getMaterialUnit().get(mdMaterial.getMaterialUnit());
            materialStandard = mdMaterial.getStandard();
            bomProperty = DataBox.getBomPropertyMap().get(mdMaterial.getBomProperty());
            producePreDays = mdMaterial.getProducePreDays().toString();
        }catch(Exception e) {
            
        }
        
        return "{\"materialUnit\": \""+materialUnit+"\", \"materialStandard\": \""+materialStandard+"\", \"bomProperty\": \""+bomProperty+"\", \"producePreDays\": \""+producePreDays+"\"}";
    }
    
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2020-11-17 14:45:25
     * @author 
     * @param prodMpsLine
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editProdMpsLine")
    @ResponseBody
    public String editProdMpsLine(@Valid ProdMpsLine prodMpsLine, BindingResult bindingResult, Model model, RedirectAttributes attr) {
        try {
            /*
            //参数校验
            Map<String, String> errorMap = this.validateParameters(bindingResult, model);
            if(errorMap.size()>0) {
                return "forward:getProdMpsLine";
            }
            */
            
            //对当前编辑的对象初始化默认的字段
            if(prodMpsLine.getMpsLineId()==null) {
                prodMpsLine.setMpsLineCode(SnowFlake.getConcurrentId().toString());
            }
            
            //保存编辑的数据
            this.prodMpsLineService.insertOrUpdateDataObject(prodMpsLine);
            
            return "{\"result\":\"success\"}";
        }catch(Exception e) {
            return "{\"result\":\"error\"}";
        }
    }
    
    
    
    /**
     * 
     * @description 删除数据
     * @date 2020-11-17 14:45:25
     * @author 
     * @param prodMpsLine
     * @return String
     *
     */
    @RequestMapping("deleteProdMpsLine")
    @ResponseBody
    public String deleteProdMpsLine(ProdMpsLine prodMpsLine, RedirectAttributes attr) {
        try {
            //删除数据前验证数据
            if(prodMpsLine!=null&&prodMpsLine.getMpsLineId()!=null) {
                //删除数据
                this.prodMpsLineService.deleteDataObject(prodMpsLine);
            }
            
            return "{\"result\":\"success\"}";
        }catch(Exception e) {
            return "{\"result\":\"error\"}";
        }
    }
}
